package uit.edu.vn.portal.elearning.schedule;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uit.edu.vn.portal.elearning.helper.SakaiWSHelper;
import uit.edu.vn.portal.elearning.logic.SakaiWSLogic;
import uit.edu.vn.portal.elearning.logic.ScheduleWSLogic;
import uit.edu.vn.portal.elearning.models.announcement.Announcement;
import uit.edu.vn.portal.elearning.models.schedule.*;
import uit.edu.vn.portal.elearning.utils.Utils;

import com.liferay.util.bridges.mvc.MVCPortlet;

/**
 * Portlet implementation class CalendarPortlet
 */
public class SchedulePortlet extends MVCPortlet {
	
private final Log log = LogFactory.getLog(getClass().getName());
	
	private String adminUserName;
	private String adminPassword;
	private String loginUrl;
	private String scriptUrl;
	private String serviceUrl;
	private SakaiWSLogic logic;
	
	@Override
	public void init(PortletConfig config) throws PortletException {	   
		   super.init(config);
		   log.info("Start SchedulePortlet init()");

		   //get params
		   adminUserName = config.getInitParameter("sakai.admin.username");
		   adminPassword = config.getInitParameter("sakai.admin.password");
		   loginUrl = config.getInitParameter("sakai.ws.login.url");
		   scriptUrl = config.getInitParameter("sakai.ws.script.url");
		   serviceUrl = config.getInitParameter("sakai.ws.service.url");
		   
		   log.info("End SchedulePortlet init()");	   
		}
	
	@Override
	public void doView(RenderRequest renderRequest,
			RenderResponse renderResponse) throws IOException, PortletException {
		// TODO Auto-generated method stub
		initSakaiWSLogic(renderRequest);
		List<CalendarEvent> schedules = SakaiWSHelper.getSchedules(logic);
		renderRequest.setAttribute("schedules", schedules);
		renderRequest.setAttribute("logic", logic);

		super.doView(renderRequest, renderResponse);
	}
	
	private void initSakaiWSLogic(RenderRequest renderRequest){
		   logic = new SakaiWSLogic();
		   logic.setAdminUsername(adminUserName);
		   logic.setAdminPassword(adminPassword);
		   logic.setEid(Utils.getAuthenUsername(renderRequest));
		   logic.setLoginUrl(loginUrl);
		   logic.setScriptUrl(scriptUrl);
		   logic.setServiceUrl(serviceUrl);
	}

}
